package com.action.gulimall.member.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * @author sl shilun217@qq.com
 * @Deacription 将Feign远程请求携带cookie
 * @date 2021/07/04/17:13
 **/
@Configuration
public class MyFeignConfig {
    @Bean
    public RequestInterceptor getRequestInterceptor(){
        return template -> {
           ServletRequestAttributes serveltRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (serveltRequestAttributes != null) {
                HttpServletRequest request = serveltRequestAttributes.getRequest();
                if(request!=null){
                    String cookie = request.getHeader("Cookie");
                    if(!StringUtils.isEmpty(cookie)){
                        template.header("Cookie",cookie);
                    }
                }
            }
        };
    }
}
